<%@page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>

<script type="text/javascript">
	var form_url = base.contextPath+"/sysmgr/user/add.do" ;
	var company, dept, workgroup, userrole, permits, tabsContainer ;
	$(function() {
		
		tabsContainer = $("#tabsContainer").tabs({
			fit: true, border: false
		}) ;
		
		company = $("#company").combotree();
		dept = $("#dept").combotree();
		
		workgroup = $("#workgroup").combogrid({
			url: base.contextPath+"/sysmgr/workgroup/doNotNeedAuth_datagrid.do", multiple: true,
			idField:'id', textField:'name', pagination: true, panelWidth: 400, panelHeight: 200,
			columns: [[
			    { field: 'name', title: '组名称', width: 130, sortable: true },
			    { field: 'remark', title: '组描述', width: 200, sortable: true }
			]]
		});
		
		userrole = $("#userrole").combogrid({
			url: base.contextPath+"/sysmgr/userrole/doNotNeedAuth_datagrid.do", multiple: true,
			idField:'id', textField:'name', pagination: true, panelWidth: 400, panelHeight: 200,
			columns: [[
			    { field: 'name', title: '角色名称', width: 130, sortable: true },
			    { field: 'remark', title: '角色描述', width: 200, sortable: true }
			]]
		});
		
		permits = $("#permits").combogrid({
			url: base.contextPath+"/sysmgr/permits/doNotNeedAuth_datagrid.do", multiple: true,
			idField:'id', textField:'name', pagination: true, panelWidth: 400, panelHeight: 200,
			columns: [[
			    { field: 'name', title: '权限组名称', width: 130, sortable: true },
			    { field: 'remark', title: '权限组描述', width: 200, sortable: true }
			]]
		});
		
		//编辑，加载表单数据
		if($('input[name=id]').val().length > 0) {
			form_url = base.contextPath+"/sysmgr/user/edit.do" ;
			$.post(base.contextPath+"/sysmgr/user/get.do", {id:$('input[name=id]').val()}, function(result) {
				if (result.id != undefined) {
					$('form').form('load', {
						'num' : result.num,
						'account' : result.account,
						'truename' : result.truename,
						'mobile' : result.mobile,
						'email' : result.email,
						'company_id' : result.company_id,
						'dept_id' : result.dept_id,
						'workgroup_ids' : (undefined != result.workgroup_ids?result.workgroup_ids.split(","):""),
						'userrole_ids' : (undefined != result.userrole_ids?result.userrole_ids.split(","):""),
						'permits_ids' : (undefined != result.permits_ids?result.permits_ids.split(","):"")
					});
					
					$("input[name=status][value="+result.status+"]").attr("checked", "checked") ;
					$("input[name=sex][value="+result.sex+"]").attr("checked", "checked") ;
					$("input[name=password]").attr("readonly", "readonly").val("密码不可编辑") ;
					
					company.combotree({
						url : base.contextPath+"/sysmgr/company/doNotNeedAuth_tree.do",
						editable: false, lines:true, required: false,
						onLoadSuccess: function(node, data) {
							if(data && data.length>0){
								company.combotree("setValue", result.company_id) ; 
							}
						},
						onChange: function(newValue, oldValue) { 
							dept.combotree({
			        			url : base.contextPath+"/sysmgr/dept/doNotNeedAuth_tree.do?company_id="+newValue,
			        			editable: false, lines:true,
			        			onLoadSuccess: function(node, data) {
			        				if(result.dept_id){
			        					dept.combotree("setValue", result.dept_id) ; 
			        				}
			        			}
			        	    });
			            }
				    });
				}
			}, 'json');
		} else {
			company.combotree({
				url : base.contextPath+"/sysmgr/company/doNotNeedAuth_tree.do",
				editable: false, lines:true, required: false,
				onLoadSuccess: function(node, data) {
					if(data && data.length>0){
						company.combotree("setValue", data[0].id) ; 
					}
				},
				onChange: function(newValue, oldValue) { 
					dept.combotree({
	        			url : base.contextPath+"/sysmgr/dept/doNotNeedAuth_tree.do?company_id="+newValue,
	        			editable: false, lines:true,
	        			onLoadSuccess: function(node, data) {
	    					if(data && data.length>0){
	    						dept.combotree("setValue", data[0].id) ; 
	    					}
	    				},
	        	    });
	            }
		    });
		}
	});
	
	
	//提交表单数据
	var submitNow = function($d, $dg, flag) {
		$.post(form_url, $("#form").form("getData"), function(result) {
			if (result.status) {
				$dg.datagrid('reload');alertify.success(result.msg);$.easyui.loaded();
				if(flag==undefined||flag==false){$d.dialog("close") ;}
			} else {
				$.easyui.loaded();$.messager.alert("错误", result.msg, "error");
			}
		}, 'json').error(function() {$.easyui.loaded();});
	};
	
	//验证表单
	var submitForm = function($d, $dg, flag) { 
		if($('#form').form('validate')) {
			$.easyui.loading({ msg: "数据提交中，请稍等..." });
			submitNow($d, $dg, flag) ;
		}
	};
	
	
</script>

<div id="tabsContainer">
	<div class="panel-container" data-options="title: '基本信息', iconCls: 'icon-standard-layout-header', refreshable: false, selected: true">
		<form id="form" class="easyui-form form_container">
			<input type="hidden" name="id" value="${id}" />
			<input type="hidden" name="modifyName" value="${USER_SESSION.truename}" />
			<table class="tableform">
				<tr>
					<th>用户编号：</th>
					<td><input name="num" class="easyui-validatebox" type="text" /></td>
					<th>性别：</th>
					<td>
						<div id="radio">
							<input type="radio" name="sex" value="男" checked /><span>男</span>
							<input type="radio" name="sex" value="女" /><span>女</span>
						</div>
					</td>
				</tr>
				<tr>
					<th>登录账号：</th>
					<td><input name="account" class="easyui-validatebox" type="text" /></td>
					<th>登录密码：</th>
					<td><input name="password" class="easyui-validatebox" type="text" /></td>
				</tr>
				<tr>
					<th>真实姓名：</th>
					<td><input name="truename" class="easyui-validatebox" type="text" /></td>
					<th>手机号码：</th>
					<td><input name="mobile" class="easyui-validatebox" type="text" /></td>
				</tr>
				<tr>
					<th>邮箱地址：</th>
					<td><input name="email" class="easyui-validatebox" type="text" /></td>
					<th>手机号码：</th>
					<td><input name="a" class="easyui-validatebox" type="text" /></td>
				</tr>
				<tr>
					<th>访问控制：</th>
					<td colspan="3">
						<span id="radio">
							<input type="radio" name="status" value="0" checked /><span>允许登录</span>
							<input type="radio" name="status" value="1" /><span>禁止登录</span>
						</span>
						<span id="checkbox">
							<input type="checkbox" name="access" value="0" checked /><span>OA首页</span>
							<input type="checkbox" name="access" value="1" /><span>人员列表</span>
							<input type="checkbox" name="access" value="2" /><span>邮件发送</span>
						</span>
					</td>
				</tr>
				<tr>
					<th>隶属机构：</th>
					<td><input id="company" name="company_id" style="width:218px;height:30px;" /><a onClick="company.combotree('setValue','');" class="easyui-linkbutton" data-options="plain: true, iconCls: 'ext_clear'"></a></td>
					<th>所属部门：</th>
					<td><input id="dept" name="dept_id" style="width:218px;height:30px;" /><a onClick="dept.combotree('setValue','');" class="easyui-linkbutton" data-options="plain: true, iconCls: 'ext_clear'"></a></td>
				</tr>
				<tr>
					<th>工作组：</th>
					<td>
						<input id="workgroup" name="workgroup_ids" style="width:218px;height:30px;" /><a onClick="workgroup.combogrid('setValue','');" class="easyui-linkbutton" data-options="plain: true, iconCls: 'ext_clear'"></a>
					</td>
					<th>角色：</th>
					<td>
						<input id="userrole" name="userrole_ids" style="width:218px;height:30px;" /><a onClick="userrole.combogrid('setValue','');" class="easyui-linkbutton" data-options="plain: true, iconCls: 'ext_clear'"></a>
					</td>
				</tr>
				<tr>
					<th>权限组：</th>
					<td>
						<input id="permits" name="permits_ids" style="width:218px;height:30px;" /><a onClick="permits.combogrid('setValue','');" class="easyui-linkbutton" data-options="plain: true, iconCls: 'ext_clear'"></a>
					</td>
				</tr>
			</table>
		</form>
	</div>
	
	<div class="panel-container" data-options="title: '其他信息', iconCls: 'icon-standard-layout-header', refreshable: false">
		
	</div>
</div>
